<template>
    <div class="chat-container">
      <h1>反馈意见聊天室</h1>
      <div class="chat-box" ref="chatBox">
        <div v-for="(message, index) in messages" :key="index" class="message">
          <div class="message-content" :class="{'self': message.isSelf}">
            <img v-if="message.avatar" :src="message.avatar" alt="Avatar" class="avatar" />
            <div class="text">{{ message.text }}</div>
          </div>
        </div>
      </div>
      <div class="input-area">
        <input v-model="newMessage" @keyup.enter="sendMessage" placeholder="输入消息..." />
        <button @click="sendMessage">发送</button>
      </div>
    </div>
  </template>
  
  <script>
  import io from 'socket.io-client';
  
  export default {
    data() {
      return {
        socket: null,
        messages: [],
        newMessage: '',
        userAvatar: 'path/to/user-avatar.png', // 用户头像路径
        adminAvatar: 'path/to/admin-avatar.png' // 管理员头像路径
      };
    },
    created() {
      // 连接到 WebSocket 服务器
      this.socket = io('http://localhost:3000'); // 请替换为你的 WebSocket 服务器地址
  
      // 监听消息事件
      this.socket.on('message', (msg) => {
        this.messages.push({ text: msg, isSelf: false, avatar: this.adminAvatar });
        this.scrollToBottom();
      });
    },
    methods: {
      sendMessage() {
        if (this.newMessage.trim() !== '') {
          this.socket.emit('message', this.newMessage);
          this.messages.push({ text: this.newMessage, isSelf: true, avatar: this.userAvatar });
          this.newMessage = '';
          this.scrollToBottom();
        }
      },
      scrollToBottom() {
        this.$nextTick(() => {
          const chatBox = this.$refs.chatBox;
          chatBox.scrollTop = chatBox.scrollHeight;
        });
      }
    },
    beforeDestroy() {
      // 断开 WebSocket 连接
      if (this.socket) {
        this.socket.disconnect();
      }
    }
  };
  </script>
  
  <style scoped>
  .chat-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    padding: 20px;
  }
  
  .chat-box {
    border: 1px solid #ccc;
    padding: 10px;
    flex: 1;
    overflow-y: scroll;
    margin-bottom: 10px;
  }
  
  .message {
    margin-bottom: 10px;
    display: flex;
    align-items: flex-start;
  }
  
  .message-content {
    max-width: 70%;
    padding: 10px;
    border-radius: 10px;
    background-color: #f0f0f0;
    display: flex;
    align-items: flex-start;
  }
  
  .message-content.self {
    background-color: #007bff;
    color: white;
    margin-left: auto;
    align-items: flex-end;
  }
  
  .message-content.self .avatar {
    order: 2;
    margin-left: 10px;
  }
  
  .avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 10px;
  }
  
  .text {
    flex: 1;
  }
  
  .input-area {
    display: flex;
    align-items: center;
  }
  
  .input-area input {
    flex: 1;
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 5px;
    margin-right: 10px;
  }
  
  .input-area button {
    padding: 10px 20px;
    border: none;
    border-radius: 5px;
    background-color: #007bff;
    color: white;
    cursor: pointer;
  }
  
  .input-area button:hover {
    background-color: #0056b3;
  }
  </style>